package com.gitee.hljdrl.ffkit.app;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gitee.hljdrl.ffkit.FFKit;
import com.gitee.hljdrl.ffkit.R;
import com.gitee.hljdrl.ffkit.FFKitApplication;
import com.gitee.hljdrl.ffkit.adapter.BaseRvAdapter;
import com.gitee.hljdrl.ffkit.adapter.FFKitAdapter;
import com.gitee.hljdrl.ffkit.bean.FFKitLink;
import com.gitee.hljdrl.ffkit.builder.FFKitLinkSource;
import com.gitee.hljdrl.ffkit.databinding.FfkitActivtyBinding;
import com.gitee.hljdrl.ffkit.divider.FFKitGridDivider;
import com.gitee.hljdrl.ffkit.listener.OnExecuteListener;
import com.gitee.hljdrl.utils.collection.CollectionUtil;

import java.util.ArrayList;
import java.util.List;

public class FFKitHomeActivity extends FFKitActivity {

    private FfkitActivtyBinding mBinding;

    @Override
    public View getBindingView() {
        mBinding = FfkitActivtyBinding.inflate(getLayoutInflater());
        return mBinding.getRoot();
    }

    @Override
    public void initView() {
        Toolbar toolbar = findViewById(R.id.tool_bar);
        toolbar.setTitle("Home");
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);//这2行是设置返回按钮的
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
        toolbar.getNavigationIcon().setTint(Color.WHITE);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        //-----------------------------------------------------------------------
        Class<? extends FFKitLinkSource> cls;
        FFKitApplication ffKitApplication = FFKitApplication.getInstance();
        if (ffKitApplication != null) {
            cls = ffKitApplication.getLinkSource();
        } else {
            cls = FFKit.FACTORY_LINK_SOURCE_CLASS;
        }
        //-----------------------------------------------------------------------
        if (cls != null) {
            FFKitLinkSource homeDataSource = null;
            try {
                homeDataSource = cls.newInstance();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
            if (homeDataSource == null) {
                return;
            }
            //------------------------------------------
            homeDataSource.setActivity(getActivity());
            boolean addInList = homeDataSource.isOverlayList();
            List<FFKitLink> list = homeDataSource.getList();

            List<FFKitLink> assemblyKitList = new ArrayList<>();
            if (!addInList) {
                assemblyKitList.add(new FFKitLink.Builder().setName("Toast").setOnExecuteListener(new OnExecuteListener() {
                    @Override
                    public void onExecute(Activity activity) {
                        Toast.makeText(activity, "onExecute", Toast.LENGTH_LONG).show();
                    }
                }).build());
            }
            if (CollectionUtil.isEnable(list)) {
                assemblyKitList.addAll(list);
            }
            //
            FFKitAdapter adapter = new FFKitAdapter(this, assemblyKitList);
            RecyclerView.LayoutManager layoutManager = homeDataSource.getLayoutManager(this);
            if (layoutManager != null) {
                mBinding.rvList.setLayoutManager(layoutManager);
            } else {
                mBinding.rvList.setLayoutManager(new LinearLayoutManager(this));
            }
            mBinding.rvList.setAdapter(adapter);
            mBinding.rvList.addItemDecoration(new FFKitGridDivider(this));
            adapter.setOnItemClickListener(new BaseRvAdapter.OnItemClickListener<FFKitLink>() {
                @Override
                public void onItemClick(View v, int position, FFKitLink kitLink) {
                    if (kitLink.activityClass != null) {
                        startActivity(getActivity(), kitLink.activityClass, kitLink.bundle);
                    } else if (kitLink.onExecuteListener != null) {
                        try {
                            kitLink.onExecuteListener.onExecute(getActivity());
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    } else {
                        FFSupporterActivity.startLink(getActivity(), kitLink);
                    }
                }
            });
        }
    }

    @Override
    public void initListener() {

    }

    public static void startActivity(Activity activity, Class<?> cls) {
        startActivity(activity, cls, null);
    }

    public static void startActivity(Activity activity, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(activity, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        activity.startActivity(intent);
    }

    public static void startActivityForResult(Activity activity, Class<?> cls, int requestCode) {
        startActivityForResult(activity, cls, requestCode, null);
    }

    public static void startActivityForResult(Activity activity, Class<?> cls, int requestCode, Bundle bundle) {
        Intent intent = new Intent(activity, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        activity.startActivityForResult(intent, requestCode);
    }
}
